package com.gwd.task.services.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.gwd.task.componts.Cache;
import com.gwd.task.entity.TestProcessEntity;
import com.gwd.task.model.TestProcessDTO;
import com.gwd.task.repository.TestProcessRepository;
import com.gwd.task.services.TestProcessServer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhiyongming
 * @date 2020/12/16 14:39
 */
@Service
public class TestProcessServerImpl implements TestProcessServer {

    @Resource
    private Cache cache;
    @Resource
    private TestProcessRepository testProcessRepository;

    @Override
    public List<TestProcessDTO> findAll() {
        return testProcessRepository.findAll().stream().map(TestProcessDTO::new).collect(Collectors.toList());
    }

    @Override
    public List<TestProcessDTO> findByTestId(Long id) {
        return testProcessRepository.findByTestId(id).stream().map(TestProcessDTO::new).collect(Collectors.toList());
    }

    @Override
    public List<TestProcessDTO> findByTestIdList(List<Long> idList) {
        List<TestProcessDTO> dtoList =
                testProcessRepository.findByTestIdIn(idList).stream().map(TestProcessDTO::new).collect(Collectors.toList());
        cache.matchUserName(dtoList, "getOperationUser", "setOperationUserName");
        return dtoList;
    }

    @Override
    public void save(TestProcessDTO dto) {
        TestProcessEntity entity = new TestProcessEntity();
        BeanUtil.copyProperties(dto, entity, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        testProcessRepository.save(entity);
    }

    @Override
    public void saveAll(List<TestProcessEntity> list) {
        if (list!=null) {
            testProcessRepository.saveAll(list);
        }
    }
}
